<?php
declare (strict_types=1);

namespace app\api\controller;

use think\facade\View;
use think\exception\ValidateException;
use think\response\Json;

class Security extends Base
{

    public function index(): Json
    {
        if (!$this->user->pay_password) {
            return json(['code' => -1, 'msg' => '尚未设置支付密码']);
        } else {
            return json(['code' => 1, 'msg' => '已设置支付密码']);
        }
    }


    public function save()
    {
        $param = $this->request->post('', '', $this->filter);
        $param['type'] = 'pay_password';
        if (!$this->user->pay_password) {
            $validate = 'SecurityFirst';
            $first = true;
        } else {
            $validate = 'Security';
            $first = false;
        }
        try {
            $this->validate($param, $validate);
        } catch (ValidateException $e) {
            return json(['code' => -1, 'msg' => $e->getError()]);
        }
        return json(\app\common\model\User::security($param, $this->user, $first));

    }

}

